package com.he.projects.custom.bean;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

/**
 * @author heyanjing
 */
@Slf4j
@Data
public class ResultT<T> {

    private static final int    SERVICE_ERROR = 500;
    private static final int    ARGS_ERROR    = 999;
    private static final int    NO_AUTH       = 403;
    private static final int    SUCCESS       = 0;
    /**
     * code 0 - 正确； 非0为错误码
     */
    private              int    code          = 0;
    private              String msg;
    private              T      data;


    public ResultT() {
    }

    public <D extends T> ResultT(D data) {
        this.data = data;
    }

    public ResultT(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public static <T> ResultT<T> errorWithCodeAndMsg(int code, String msg) {
        return new ResultT<>(code, msg);
    }

    public static <T> ResultT<T> success() {
        return new ResultT<>();
    }

    public static <T> ResultT<T> successWithMsg(String msg) {
        return new ResultT<>(SUCCESS, StringUtils.isBlank(msg) ? "操作成功!" : msg);
    }

    public static <T> ResultT<T> successWithDefaultMsg() {
        return successWithMsg(null);
    }

    public static <T> ResultT<T> successWithData(T data) {
        return new ResultT<>(data);
    }

    public static <T> ResultT<T> errorWithArgs(String msg) {
        return new ResultT<>(ARGS_ERROR, msg);
    }

    public static <T> ResultT<T> errorWithMsg(String msg) {
        return new ResultT<>(SERVICE_ERROR, StringUtils.isNotBlank(msg) ? msg : "网络异常，请稍后再试!");
    }

    public static <T> ResultT<T> error() {
        return errorWithMsg(null);
    }

    public static <T> ResultT<T> noAuth(String msg) {
        return new ResultT<>(NO_AUTH, StringUtils.isBlank(msg) ? "无授权!" : msg);
    }

    public static <T> ResultT<T> noAuth() {
        return noAuth(null);
    }

}
